package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.commons.Constants;
import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.model.vo.ErrorResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserFreezeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void checkUserStates(Long userId, int state) {
        String key = Constants.FREEZE_USER + userId;
        String redisJSON = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isNotEmpty(redisJSON)) {
            JSONObject jsonObject = JSON.parseObject(redisJSON);
            Integer freezingRange = jsonObject.getInteger("freezingRange");
            if(freezingRange == state) {
                throw new TanHuaException(ErrorResult.builder().errMessage("您的账号被冻结！原因:" + jsonObject.getString("reasonsForFreezing")).build());
            }
        }
    }
}
